package org.wenhuayuan.project.main.logic;

import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.fragment.app.FragmentManager;

import org.wenhuayuan.project.common.tab.HiFragmentTabView;
import org.wenhuayuan.project.common.tab.HiTabViewAdapter;
import org.wenhuayuan.project.main.R;
import org.wenhuayuan.project.main.fragment.CategoryFragment;
import org.wenhuayuan.project.main.fragment.FavoriteFragment;
import org.wenhuayuan.project.main.fragment.HomePageFragment;
import org.wenhuayuan.project.main.fragment.ProfileFragment;
import org.wenhuayuan.project.main.fragment.RecommendFragment;
import org.wenhuayuan.uilibrary.tab.bottom.HiTabBottomInfo;
import org.wenhuayuan.uilibrary.tab.bottom.HiTabBottomLayout;
import org.wenhuayuan.uilibrary.tab.common.IHiTabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainAtyLogic {
    private HiFragmentTabView hiFragmentTabView;
    private HiTabBottomLayout hiTabBottomLayout;
    private List<HiTabBottomInfo<?>> infoList;
    private ActivityProvider atyProvider;
    private int currentItemIndex;
    private final static String SAVE_CURRENT_ID = "SAVE_CURRENT_ID";

    public MainAtyLogic(ActivityProvider atyProvider, Bundle savedInstanceState) {
        this.atyProvider = atyProvider;
        if (null != savedInstanceState)
            currentItemIndex = savedInstanceState.getInt(SAVE_CURRENT_ID);
        initTabBottom();
    }

    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(SAVE_CURRENT_ID, currentItemIndex);
    }

    // 初始化底部tab导航栏
    private void initTabBottom() {
        hiTabBottomLayout = atyProvider.findViewById(R.id.tab_bottom_layout);
        hiTabBottomLayout.setAlpha(0.8f);
        infoList = new ArrayList<>();
        int defaultColor = atyProvider.getResources().getColor(R.color.tabBottomDefaultColor);
        int selectedColor = atyProvider.getResources().getColor(R.color.tabBottomTintColor);
        HiTabBottomInfo homeInfo = new HiTabBottomInfo<Integer>(
                "首页",
                "fonts/iconfont.ttf",
                atyProvider.getResources().getString(R.string.if_home),
                null,
                defaultColor,
                selectedColor);
        HiTabBottomInfo infoFavorite = new HiTabBottomInfo<Integer>(
                "收藏",
                "fonts/iconfont.ttf",
                atyProvider.getResources().getString(R.string.if_favorite),
                null,
                defaultColor,
                selectedColor);
        HiTabBottomInfo infoCategory = new HiTabBottomInfo<Integer>(
                "分类",
                "fonts/iconfont.ttf",
                atyProvider.getResources().getString(R.string.if_category),
                null,
                defaultColor,
                selectedColor);
        HiTabBottomInfo infoRecommend = new HiTabBottomInfo<Integer>(
                "推荐",
                "fonts/iconfont.ttf",
                atyProvider.getResources().getString(R.string.if_recommend),
                null,
                defaultColor,
                selectedColor);
        HiTabBottomInfo infoProfile = new HiTabBottomInfo<Integer>(
                "我的",
                "fonts/iconfont.ttf",
                atyProvider.getResources().getString(R.string.if_profile),
                null,
                defaultColor,
                selectedColor);
        homeInfo.fragment = HomePageFragment.class;
        infoRecommend.fragment = RecommendFragment.class;
        infoCategory.fragment = CategoryFragment.class;
        infoFavorite.fragment = FavoriteFragment.class;
        infoProfile.fragment = ProfileFragment.class;
        infoList.add(homeInfo);
        infoList.add(infoRecommend);
        infoList.add(infoFavorite);
        infoList.add(infoCategory);
        infoList.add(infoProfile);

        hiTabBottomLayout.inflateInfo(infoList);
        initFragmentTabView();
        hiTabBottomLayout.addTabSelectedChangeListener(new IHiTabLayout.OnTabSelectedListener<HiTabBottomInfo<?>>() {
            @Override
            public void onTabSelectedChange(int index, HiTabBottomInfo<?> prevInfo, @NonNull HiTabBottomInfo<?> nextInfo) {
                MainAtyLogic.this.currentItemIndex = index;
                hiFragmentTabView.setCurrentItem(index);
            }
        });
        hiTabBottomLayout.defaultSelected(infoList.get(currentItemIndex));

    }

    private void initFragmentTabView() {
        hiFragmentTabView = atyProvider.findViewById(R.id.fragment_tab_view);
        HiTabViewAdapter tabViewAdapter = new HiTabViewAdapter(atyProvider.getSupportFragmentManager(), infoList);
        hiFragmentTabView.setViewAdapter(tabViewAdapter);
    }

    public HiFragmentTabView getHiFragmentTabView() {
        return hiFragmentTabView;
    }

    public HiTabBottomLayout getHiTabBottomLayout() {
        return hiTabBottomLayout;
    }

    public List<HiTabBottomInfo<?>> getInfoList() {
        return infoList;
    }

    public int getCurrentItemIndex() {
        return currentItemIndex;
    }

    public interface ActivityProvider{
        <T extends View> T findViewById(@IdRes int id);
        Resources getResources();
        FragmentManager getSupportFragmentManager();
        String getString(@StringRes int resId);
    }
}
